package jude.java.money.migrate;

import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.util.Utils;

/** magento custom options */
public class CustomOption {
	//text
	public static final String TYPE_FIELD = "field";
	public static final String TYPE_AREA = "area";
	//file
	public static final String TYPE_FILE = "file";
	//select
	public static final String TYPE_DROP_DOWN = "drop_down";
	public static final String TYPE_RADIO = "radio";
	public static final String TYPE_CHECKBOX = "checkbox";
	public static final String TYPE_MULTIPLE = "multiple";
	//date
	public static final String TYPE_DATE = "date";
	public static final String TYPE_DATE_TIME = "date_time";
	public static final String TYPE_TIME = "time";

	private String title;
	private String type;
	private boolean require;
	private String sku;
	private String value;
	private boolean configure;
	private CustomOptionItem[] items;
	
	public CustomOption() {
	}
	public CustomOption(String title, String type, boolean require, String sku) {
		this.title = title;
		this.type = type;
		this.require = require;
		this.sku = sku;
	}
	public CustomOption(String title, CustomOptionItem[] items) {
		this.title = title;
		this.type = TYPE_DROP_DOWN;
		this.require = true;
		this.items = items;
	}
	
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public boolean isRequire() {
		return require;
	}
	public void setRequire(boolean require) {
		this.require = require;
	}
	public String getSku() {
		return sku;
	}
	public void setSku(String sku) {
		this.sku = sku;
	}
	public CustomOptionItem[] getItems() {
		return items;
	}
	public void setItems(CustomOptionItem[] items) {
		this.items = items;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getValue() {
		return value;
	}
	public boolean isConfigure() {
		return configure;
	}
	public CustomOption setConfigure(boolean configure) {
		this.configure = configure;
		return this;
	}
	public CustomOption sku(String sku) {
		this.sku = MigrateUtils.sku(sku);
		return this;
	}
	
	public boolean isSelect() {
		return TYPE_DROP_DOWN.equals(type) || TYPE_RADIO.equals(type) || TYPE_CHECKBOX.equals(type)
				|| TYPE_MULTIPLE.equals(type);
	}
	
	//Utils.in(type, new String[] {})
	public boolean setZencartType(String type) {
		if (Utils.in(type, new String[] {"Dropdown", "下拉"}))
			this.type = TYPE_DROP_DOWN;
		else if (Utils.in(type, new String[] {"Checkbox", "多选"}))
			this.type = TYPE_CHECKBOX;
		else if ("Radio".equals(type) || "单选".equals(type))
			this.type = TYPE_RADIO;
		else if (Utils.in(type, new String[] {"Text", "Read Only", "文本", "只读"}))
			this.type = TYPE_FIELD;
		else if (Utils.in(type, new String[] {"File", "文件"}))
			this.type = TYPE_FILE;
		else {
			this.type = null;
			System.out.println("unknown type: " + type);
			return false;
		}
		return true;
	}
	
	public void addOptionItem(CustomOptionItem item) {
		int size = items == null ? 0 : items.length;
		CustomOptionItem[] items = new CustomOptionItem[size + 1];
		for (int i = 0; i < size; i++) {
			items[i] = this.items[i];
		}
		items[size] = item;
		this.items = items;
	}
	
	public boolean setConfigurable(String[] configurableAttributes) {
		boolean c = false;
		if (configurableAttributes != null) {
			for (int i = 0; i < configurableAttributes.length; i++) {
				if (configurableAttributes[i].equals(this.title)) {
					c = true;
					break;
				}
			}
		}
		return this.configure = c;
	}
	
	public void sort() {
		if (items == null || items.length == 0)
			return;
		Arrays.sort(items, new Comparator<CustomOptionItem>() {
			@Override
			public int compare(CustomOptionItem o1, CustomOptionItem o2) {
				int f1 = getNumber(o1.getTitle());
				int f2 = getNumber(o2.getTitle());
				if (f1 == -1 || f2 == -1)
					return o1.getTitle().compareTo(o2.getTitle());
				return f1 - f2;
			}
		});
	}
	
	private int getNumber(String str) {
		Pattern p = Pattern.compile("(\\d+(\\.\\d+)?)");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return (int)(100 * Float.parseFloat(m.group(1)));
		}
		return -1;
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		char s = '\t';
		sb.append(type).append(s).append(title);
		sb.append(s).append(MigrateUtils.code(title));
		if (sku != null)
			sb.append(s).append(sku);
		if (value != null)
			sb.append(s).append(value);
		sb.append("\r\n");
		if (items != null)
			for (CustomOptionItem item : items) {
				sb.append('\t').append(item.getTitle()).append(s).append(item.getSku()).append("\r\n");
			}
		return sb.toString();
	}
}
